package com.lottery.ruok.lottrey.adapter;

import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.PrizeListActivity;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.retrofitModule.response.LotteryStatOneResponse;
import com.lottery.ruok.lottrey.utils.TimeUtil;
import com.nex3z.flowlayout.FlowLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.AreaCircle;
import utils.Utils;

/**
 * Created by ASUS on 2017/11/13.
 */

public class PL3AndFC3DListViewAdapter extends BaseAdapter {
    private FragmentActivity fragmentActivity;
    private LotteryStatOneResponse lotteryStatOneResponses;

    public PL3AndFC3DListViewAdapter(FragmentActivity fragmentActivity, LotteryStatOneResponse lotteryStatOneResponses) {
        this.fragmentActivity = fragmentActivity;
        this.lotteryStatOneResponses = lotteryStatOneResponses;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(fragmentActivity).inflate(R.layout.prize_pl3_fc3d_listview_item, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }
        String code = lotteryStatOneResponses.getItem_code();
        String[] temp = lotteryStatOneResponses.getIssue_list().get(i).getPrize_num().split(",");
        holder.container.removeAllViews();
        if (temp != null) {
            for (String s : temp) {
                AreaCircle areaCircle = new AreaCircle(fragmentActivity);
                Common.setBallBackground(areaCircle, s, code);
                areaCircle.setText(s);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Utils.dip2px(fragmentActivity, 20), Utils.dip2px(fragmentActivity, 20));
                areaCircle.setLayoutParams(layoutParams);
                holder.container.addView(areaCircle);
            }
        }
        LotteryStatOneResponse.IssueListBean issueListBean = lotteryStatOneResponses.getIssue_list().get(i);
        String[] zonghe = issueListBean.getZonghe().split(",");
        if (lotteryStatOneResponses.getItem_code().contains("js")) {
            holder.timeNo.setText(TimeUtil.getDayFormatTime(TimeUtil.HOUR_MINUTE_DATE_FORMAT,issueListBean.getIssue_date()));
        } else {
            holder.timeNo.setText(TimeUtil.getDayFormatTime(TimeUtil.MONTH_DAY_DATE_FORMAT,issueListBean.getIssue_date()));
        }

        holder.periods.setText(issueListBean.getIssue_no());
        holder.sum.setText(zonghe[0]);
        holder.status.setText(zonghe[1]+" "+zonghe[2]);
        return view;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public int getCount() {
        if (lotteryStatOneResponses != null) {
            return lotteryStatOneResponses.getIssue_list().size();
        }
        return 0;
    }

    class ViewHolder {
        @BindView(R.id.time_no)
        TextView timeNo;
        @BindView(R.id.periods)
        TextView periods;
        @BindView(R.id.container)
        FlowLayout container;
        @BindView(R.id.sum)
        TextView sum;
        @BindView(R.id.status)
        TextView status;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}